package Main;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Mysql {

	private static Mysql instance = null;
    private Connection conn = null;
    private Statement  smt  = null;
    private String host = "blneige";
    private String login = "repco11";
    private String pass = "repco11";
    private String base = "repco11";   
    
 
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private Mysql() {
        try {
            conn = DriverManager.getConnection("jdbc:mysql://" + host + "/"
                    + base + "?" + "user=" + login + "&password=" + pass);
            smt = conn.createStatement();
            
    		System.out.println("\t-->Créée");
    		
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static Mysql getInstance() {
    	if (instance == null) {
    		System.out.println("Base de données...");
    		instance = new Mysql();
    	}
    	return instance;
    }
    
    public void closeMysql(){
    	try {
			System.out.println("Base de données...");
			conn.close();
			System.out.println("\t-->Fermée");
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }

    public boolean executeInsert(String query) {
        try {
            return smt.execute(query);
        } catch (SQLException e) {
            System.err.println(query);
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean executeSpecialInsert(String query) throws SQLException {
        return smt.execute(query);
    }

    public int executeUpdate(String query) {
        try {
            return smt.executeUpdate(query);
        } catch (SQLException e) {
    e.printStackTrace();
            System.err.println(e.getSQLState());
            System.err.println(e.getMessage());
            return -1;
        }
    }

    public ResultSet executeSelect(String query) {
        try {
            return smt.executeQuery(query);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
	}
    }

    public List<List> getResultSet(ResultSet r) {
       	
    	List<String> listeStr = new ArrayList<String>();
    	List<List> liste = new ArrayList<List>();
    	
        try {
            r.beforeFirst();
            ResultSetMetaData meta = r.getMetaData();
            int nbCol = meta.getColumnCount();
            
/*            for (int i = 1; i < nbCol + 1; ++i) {
                if (i > 1)
                    System.out.print(", ");
                System.out.print(meta.getColumnLabel(i));
            }
            System.out.println("");
*/
            while (r.next()) {
            	listeStr= new ArrayList<String>();
                for (int i = 1; i < nbCol + 1; ++i) {
                	listeStr.add(r.getString(i));
                }
                liste.add(listeStr);
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return liste;
    }
    


    public List<String> getSColumnFromResultSet(ResultSet r, String col) {
        List<String> l = new ArrayList<String>();
        try {
            r.beforeFirst();
            while (r.next())
                l.add(r.getString(col));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return (l);
    }
    
    public void supprimerTout () {
    	String query = "TRUNCATE TABLE PositionTerme;";
    	executeUpdate(query);
    	query = "TRUNCATE TABLE TermeNoeud;";
    	executeUpdate(query);
    	query = "TRUNCATE TABLE Termes;";
    	executeUpdate(query);
    	query = "TRUNCATE TABLE NoeudNoeud;";
    	executeUpdate(query);
    	query = "TRUNCATE TABLE DocNoeud;";
    	executeUpdate(query);
    	query = "TRUNCATE TABLE Noeuds;";
    	executeUpdate(query);
    	query = "TRUNCATE TABLE Documents;";
    	executeUpdate(query);
    }
}
